package com.ruoyi.web.controller.news;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.news.EvaluationAgencyInfo;
import com.ruoyi.system.domain.news.EvaluationPersonInfo;
import com.ruoyi.system.domain.news.dto.EvaluationAgencyInfoDto;
import com.ruoyi.system.domain.news.query.EvaluationAgencyInfoQuery;
import com.ruoyi.system.domain.news.vo.EvaluationAgencyInfoVo;
import com.ruoyi.system.service.news.EvaluationAgencyInfoService;
import com.ruoyi.system.service.news.EvaluationPersonInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/news/evaluationAgencyInfo")
@Api(value = "evaluationAgencyInfoController", tags  = "机构信息")
public class EvaluationAgencyInfoController extends BaseController {

	@Resource
	private EvaluationAgencyInfoService evaluationAgencyInfoService;
	@Resource
	private EvaluationPersonInfoService evaluationPersonInfoService;

    @ApiOperation(value = "新增/修改机构信息信息", notes = "新增/修改机构信息信息")
	@PostMapping("/submit")
	public R submit(@RequestBody EvaluationAgencyInfoDto evaluationAgencyInfoDto) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		EvaluationAgencyInfo evaluationAgencyInfo = new EvaluationAgencyInfo();
		BeanUtils.copyProperties(evaluationAgencyInfoDto,evaluationAgencyInfo);
		if (evaluationAgencyInfo.getId()==null) {
			evaluationAgencyInfo.setCreateBy(loginUser.getUsername());
			evaluationAgencyInfo.setDeptId(loginUser.getDeptId());
			evaluationAgencyInfo.setCreateTime(new Date());
			evaluationAgencyInfo.setUpdateBy(loginUser.getUsername());
			evaluationAgencyInfo.setUpdateTime(new Date());
		} else {
			evaluationAgencyInfo.setUpdateBy(loginUser.getUsername());
			evaluationAgencyInfo.setUpdateTime(new Date());
		}
		boolean result = evaluationAgencyInfoService.saveOrUpdate(evaluationAgencyInfo);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "删除机构信息信息", notes = "删除机构信息信息")
	@DeleteMapping("/delete")
	public R delete(@RequestParam(value = "id") Long id) {
		boolean result = evaluationAgencyInfoService.removeById(id);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "获取单个机构信息信息", notes = "获取单个机构信息信息")
	@GetMapping("/getById")
	public R<EvaluationAgencyInfoVo> getById(@RequestParam(value = "id") Long id) {
		EvaluationAgencyInfoQuery evaluationAgencyInfoQuery = new EvaluationAgencyInfoQuery();
		evaluationAgencyInfoQuery.setId(id);
		List<EvaluationAgencyInfoVo> evaluationAgencyInfoList = evaluationAgencyInfoService.getEvaluationAgencyInfoList(evaluationAgencyInfoQuery);
		if (evaluationAgencyInfoList!=null&&evaluationAgencyInfoList.size()>0) {
			EvaluationAgencyInfoVo evaluationAgencyInfoVo = evaluationAgencyInfoList.get(0);
			List<EvaluationPersonInfo> evaluationPersonInfoList = evaluationPersonInfoService.list(Wrappers.<EvaluationPersonInfo>query().eq("agency_id",id).eq("is_enable",1));
			if (evaluationAgencyInfoVo!=null) {
				evaluationAgencyInfoVo.setEvaluationPersonInfoList(evaluationPersonInfoList);
			}
			return R.ok(evaluationAgencyInfoVo);
		} else {
			return R.fail("数据不存在");
		}
	}

	@ApiOperation(value = "获取机构信息信息列表")
	@PostMapping(value = "/list")
	public R<PageInfo<EvaluationAgencyInfoVo>> list(@RequestBody EvaluationAgencyInfoQuery evaluationAgencyInfoQuery) {
		List<EvaluationAgencyInfoVo> evaluationAgencyInfoList = evaluationAgencyInfoService.getEvaluationAgencyInfoList(evaluationAgencyInfoQuery);
		return R.ok(new PageInfo<>(evaluationAgencyInfoList));
	}

	@ApiOperation(value = "导入机构信息模板下载")
	@PostMapping("/importTemplate")
	public void importTemplate(HttpServletResponse response)
	{
		ExcelUtil<EvaluationAgencyInfo> util = new ExcelUtil<EvaluationAgencyInfo>(EvaluationAgencyInfo.class);
		util.importTemplateExcel(response, "机构信息");
	}

	@ApiOperation(value = "导入机构信息数据")
	@PostMapping("/importData")
	public R importData(MultipartFile file) throws Exception
	{
		ExcelUtil<EvaluationAgencyInfo> util = new ExcelUtil<EvaluationAgencyInfo>(EvaluationAgencyInfo.class);
		List<EvaluationAgencyInfo> evaluationAgencyInfoList = util.importExcel(file.getInputStream());
		if (evaluationAgencyInfoList.isEmpty()) {
			return R.fail("请填写数据");
		}
		evaluationAgencyInfoService.saveBatch(evaluationAgencyInfoList);
		return R.ok("导入成功");
	}

}
